package com.androidTool;

import static android.content.Context.ACTIVITY_SERVICE;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.example.androidbrowserpro.MyApplication;
import com.example.androidbrowserpro.WebViewActivity;

/**
 * 开机自启动
 */
public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION.equals(intent.getAction())) {
            SharedPreferences sp=MyApplication.getContext().getSharedPreferences("LocalConfig",Context.MODE_PRIVATE);
            String isOpen=sp.getString("ISOPEN","YES");
            Toast.makeText(MyApplication.getContext(),isOpen,Toast.LENGTH_SHORT).show();
            if(isOpen.equals("NO")){
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
                String packageName=MyApplication.getContext().getPackageName();
                ActivityManager manager = (ActivityManager) MyApplication.getContext().getSystemService(ACTIVITY_SERVICE);
                manager.killBackgroundProcesses(packageName);
                return;
            }
            Intent newIntent = new Intent(context, WebViewActivity.class);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
            Toast.makeText(context, "开机完毕~", Toast.LENGTH_LONG).show();
        }
    }
}